﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ConditionalAppearance;

namespace DomainComponents.XCRM {
    public enum OrderStatus {
        None = 0,
        [DisplayName("No Money")]
        NoMoney = 1,
        [DisplayName("Cusomer Canceled")]
        CustomerCanceled = 2
    }

    [DomainComponent]
    public interface IOrderCustomer : ICustomer {
        [BackReferenceProperty("BackRefCustomer")]
        IList<IOrder> Orders { get; }
    }

    [DomainComponent]
    public interface IOrderOpportunity : IOpportunity {
        [BackReferenceProperty("BackRefOpportunity")]
        IList<IOrder> Orders { get; }
    }

    [DomainComponent]
    public interface IOrderTarget : IInvoice {
        [VisibleInListView(false)]
        IOrder SourceOrder { get; set; }
    }

    [DomainComponent]
    [VisibleInReports]
    [ImageName("BO_Order")]
    [Appearance("Disable IOrder by StatusReason", TargetItems = "*", Criteria = "Status != 'None'", Enabled = false)]
    [ListViewFilter("Closed Orders", "[Status] != 'None'", "Closed Orders", Index = 0)]
    [ListViewFilter("My Open Orders", "[Owner] = custom('CurrentUserId') AND [Status] = 'None'", "My Open Orders", true, Index = 1)]
    [ListViewFilter("Open Orders", "[Status] == 'None'", "Open Orders", Index = 2)]
    public interface IOrder : ISaleBase, IOpportunityTarget {
        OrderStatus Status { get; set; }

        [VisibleInListView(false)]
        DateTime CancelDate { get; set; }

        [VisibleInListView(false)]
        IQuote SourceQuote { get; set; }

        [System.ComponentModel.Browsable(false)]
        [VisibleInListView(false)]
        IOrderCustomer BackRefCustomer { get; set; }//TODO: duplicated with IInvoce, IOpportunity, IOrder, IQuote

        [System.ComponentModel.Browsable(false)]
        [VisibleInListView(false)]
        IOrderOpportunity BackRefOpportunity { get; set; }//TODO

        [BackReferenceProperty("SourceOrder")]
        IList<IOrderTarget> Invoices { get; }
        
        [Action(PredefinedCategory.RecordEdit, Caption = "Cancel Order...", AutoCommit = true,
            TargetObjectsCriteria = "Status = 'None'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Cancel(CancelOrderParameters parameters);
        
        [Action(PredefinedCategory.RecordEdit, Caption = "Create Invoice...", AutoCommit = true,
          SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void CreateInvoiceFromOrder();
        
        [CreateInstance(typeof(IInvoice))]
        IInvoice CreateInvoice();
    }

    [DomainLogic(typeof(IOrder))]
    public class OrderLogic {
        public static void CreateInvoiceFromOrder(IOrder order) {
            IOrderTarget invoice = (IOrderTarget)order.CreateInvoice();
            order.Copy(invoice);
            order.Invoices.Add(invoice);
        }
        public static void AfterChange_BackRefCustomer(IOrder saleBase) {
            saleBase.PotentialCustomer = saleBase.BackRefCustomer;
        }
        public static void AfterChange_PotentialCustomer(IOrder saleBase) {
            saleBase.BackRefCustomer = (IOrderCustomer)saleBase.PotentialCustomer;
        }
        public static void AfterChange_BackRefOpportunity(IOrder saleBase) {
            saleBase.SourceOpportunity = saleBase.BackRefOpportunity;
        }
        public static void AfterChange_SourceOpportunity(IOrder saleBase) {
            saleBase.BackRefOpportunity = (IOrderOpportunity)saleBase.SourceOpportunity;
        }
    }
}
